package com.jiantong.user.impl.manager;

import com.google.common.collect.Maps;
import com.jiantong.common.util.MD5Util;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.dto.RetailBreedFactoryDto;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.impl.dao.*;
import com.jiantong.user.model.*;
import com.jiantong.user.enums.AppUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by mz on 10/7/17.
 *
 */
@Component
public class RetailBreedFactoryManager {

    private final UserDao userDao;

    private final UserProfileDao userProfileDao;

    private final BreedFactoryDao breedFactoryDao;

    private final OrganizationDao organizationDao;

    private final OrganizationUserDao organizationUserDao;

    private final GovernmentUserDutyDao governmentUserDutyDao;

    @Autowired
    public RetailBreedFactoryManager(UserDao userDao,
                                     UserProfileDao userProfileDao,
                                     BreedFactoryDao breedFactoryDao,
                                     OrganizationDao organizationDao,
                                     OrganizationUserDao organizationUserDao,
                                     GovernmentUserDutyDao governmentUserDutyDao) {
        this.userDao = userDao;
        this.userProfileDao = userProfileDao;
        this.breedFactoryDao = breedFactoryDao;
        this.organizationDao = organizationDao;
        this.organizationUserDao = organizationUserDao;
        this.governmentUserDutyDao = governmentUserDutyDao;
    }

    @Transactional
    public Map<String ,Object> createRetailBreedFactory(RetailBreedFactoryDto retailBreedFactoryDto){

        String realName = retailBreedFactoryDto.getRealName();

        User user = retailBreedFactoryDto.getUser();
        BreedFactory breedFactory = retailBreedFactoryDto.getBreedFactory();
        user.setStatus(-2);
        user.setPassword(MD5Util.encrypt(user.getPassword()));
        userDao.create(user);

        UserProfile userProfile = new UserProfile();
        userProfile.setGender(0);
        userProfile.setUserId(user.getId());
        userProfile.setRealName(realName);
        userProfile.setProvince(breedFactory.getProvince());
        userProfile.setProvinceId(breedFactory.getProvinceId().intValue());
        userProfile.setCity(breedFactory.getCity());
        userProfile.setCityId(breedFactory.getCityId().intValue());
        userProfile.setRegion(breedFactory.getRegion());
        userProfile.setRegionId(breedFactory.getRegionId().intValue());
        userProfile.setStreet(breedFactory.getStreet());
        userProfile.setStreetId(breedFactory.getStreetId().intValue());
        userProfile.setVillage(breedFactory.getVillage());
        userProfile.setVillageId(breedFactory.getVillageId().intValue());
        userProfileDao.create(userProfile);

        Organization organization = new Organization();
        organization.setName(breedFactory.getName());
        organization.setUserId(user.getId());
        organization.setStatus(1);
        organization.setType(1);
        organizationDao.create(organization);

        breedFactory.setId(organization.getId());
        breedFactory.setUserId(user.getId());
        breedFactoryDao.create(breedFactory);

        OrganizationUser organizationUser = new OrganizationUser();
        organizationUser.setUserId(user.getId());
        organizationUser.setUserName(realName);
        organizationUser.setOrganizationId(organization.getId());
        organizationUser.setOrganizationName(organization.getName());
        organizationUser.setOrganizationType(organization.getType());
        organizationUserDao.create(organizationUser);

        GovernmentUserDuty governmentUserDuty = new GovernmentUserDuty();
        LoginUser loginUser = UserUtil.getCurrentUser();
        governmentUserDuty.setUserId(loginUser.getId());
        governmentUserDuty.setUserName(loginUser.getUserProfile().getRealName());
        governmentUserDuty.setFactoryOrPointId(breedFactory.getId());
        governmentUserDuty.setFactoryOrPointName(breedFactory.getName());
        governmentUserDuty.setFactoryOrPointType(3);
        governmentUserDuty.setUserAppRole(AppUserRole.PREVENTION_COORDINATOR.value());

        governmentUserDutyDao.create(governmentUserDuty);
        Map<String,Object> map = Maps.newHashMap();
        map.put("breedFactoryId",breedFactory.getId());
        return map;
    }

    @Transactional
    public Map<String , Object> updateRetailBreedFactory(RetailBreedFactoryDto retailBreedFactoryDto){
        String realName = retailBreedFactoryDto.getRealName();

        User user = retailBreedFactoryDto.getUser();

        BreedFactory breedFactory = retailBreedFactoryDto.getBreedFactory();

        Organization organization = new Organization();
        organization.setId(breedFactory.getId());
        organization.setName(breedFactory.getName());
        organization.setStatus(breedFactory.getStatus());

        organizationDao.update(organization);

        OrganizationUser organizationUser = new OrganizationUser();

        organizationUser.setOrganizationName(breedFactory.getName());
        organizationUser.setOrganizationId(breedFactory.getId());
        if(realName != null){
            UserProfile userProfile = new UserProfile();
            userProfile.setRealName(realName);
            userProfile.setUserId(user.getId());
            userProfileDao.updateByUserId(userProfile);

            organizationUser.setUserName(realName);
        }

        organizationUserDao.updateByUserId(organizationUser);

        Boolean flag = breedFactoryDao.update(breedFactory);
        Map<String , Object> map = Maps.newHashMap();
        map.put("flag",flag);
        return map;
    }
}
